package com.myfojing.foshuoliaozhibingjing;

import android.app.DialogFragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static android.R.layout.simple_spinner_dropdown_item;
import static android.R.layout.simple_spinner_item;


public class SettingActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private CheckBox notify;
    private CheckBox wifi_only;

    private EditText etClockTime;

    private RadioGroup radioGroup;
    private RadioButton ra_male;
    private RadioButton ra_female;

    private Spinner set_count_jingwen;
    private Spinner set_count_zhouyu;

    private EditText myname;

    private List<String> array_jingwen;
    private ArrayAdapter<String> adapter_jingwen;
    private List<String> array_zhouyu;
    private ArrayAdapter<String> adapter_zhouyu;

    //    TimePicker timePicker;
//    AlarmManager alarmManager

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setTitle("设置");
        // UI
        notify = (CheckBox) findViewById(R.id.notify);
        etClockTime = (EditText) findViewById(R.id.etClockTime);

        wifi_only = (CheckBox) findViewById(R.id.wifi_only);

        ra_male = (RadioButton) findViewById(R.id.ra_male);
        ra_female = (RadioButton) findViewById(R.id.ra_female);
        radioGroup = (RadioGroup) findViewById(R.id.ra_group);

        myname = (EditText) findViewById(R.id.setting_myname);

        sharedPreferences = getApplication().getSharedPreferences("profile", MODE_PRIVATE);
        boolean is_male = sharedPreferences.getBoolean("is_male", true);  // 发言人
        if (is_male) {
            radioGroup.check(R.id.ra_male);
        } else {
            radioGroup.check(R.id.ra_female);
        }

        // 读闹钟数据
        notify.setChecked(sharedPreferences.getBoolean("notify_is_on", true));
        etClockTime.setText(sharedPreferences.getString("notify_time", "20:00"));
        // 只是wifi
        wifi_only.setChecked(sharedPreferences.getBoolean("wifi_is_only", true));

        /* 经文次数 */
        set_count_jingwen = (Spinner) findViewById(R.id.set_count_jingwen);
        array_jingwen = new ArrayList<>();
        array_jingwen.add(0, "1");
        array_jingwen.add(1, "2");
        array_jingwen.add(2, "3");
        adapter_jingwen = new ArrayAdapter<>(this, simple_spinner_item, array_jingwen);
        adapter_jingwen.setDropDownViewResource(simple_spinner_dropdown_item);
        int position = adapter_jingwen.getPosition(sharedPreferences.getString("set_count_jingwen", "1"));

        set_count_jingwen.setAdapter(adapter_jingwen);
        set_count_jingwen.setSelection(position);

        /* 咒语次数 */
        set_count_zhouyu = (Spinner) findViewById(R.id.set_count_zhouyu);
        array_zhouyu = new ArrayList<>();
        array_zhouyu.add(0, "3");
        array_zhouyu.add(1, "7");
        array_zhouyu.add(2, "21");
        array_zhouyu.add(3, "108");
        adapter_zhouyu = new ArrayAdapter<>(this, simple_spinner_item, array_zhouyu);
        adapter_zhouyu.setDropDownViewResource(simple_spinner_dropdown_item);
        position = adapter_zhouyu.getPosition(sharedPreferences.getString("set_count_zhouyu", "3"));

        set_count_zhouyu.setAdapter(adapter_zhouyu);
        set_count_zhouyu.setSelection(position);

        // 我的名字
        myname.setText(sharedPreferences.getString("myname", ""));

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    // button
    public void btnApplyClicked(View view) {
        // 保存数据
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putBoolean("notify_is_on", notify.isChecked());  // 是否开启通知
        editor.putString("notify_time", etClockTime.getText().toString());  // 提醒时间
        editor.putBoolean("wifi_is_only", wifi_only.isChecked());
        RadioButton rb = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());

        editor.putString("set_count_jingwen", set_count_jingwen.getSelectedItem().toString());
        editor.putString("set_count_zhouyu", set_count_zhouyu.getSelectedItem().toString());
        editor.putString("myname", myname.getText().toString());

        boolean is_male = rb.getText().toString().equals("男声");
        editor.putBoolean("is_male", is_male);
        editor.apply();

        Toast.makeText(getApplication(), "设置已保存", Toast.LENGTH_SHORT).show();

        setResult(RESULT_OK);
        this.finish();

    }

    public void btnCancelClicked(View view) {
        setResult(RESULT_CANCELED);
        this.finish();
    }

    // 时间选择
    public void clockTimeClick(View view) {
        DialogFragment newFragment = new com.myfojing.foshuoliaozhibingjing.TimePickerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", view.getId());
        newFragment.setArguments(bundle);
        newFragment.show(getFragmentManager(), "timePicker");
    }


    // 数字选择
    public void popout_numberpicker(View view) {

    }
    // end
}
